// 基础路径 注意发布之前要先修改这里
const path = require("path");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  .BundleAnalyzerPlugin;
const child_process = require("child_process");
const PacProxyAgent = require("pac-proxy-agent");

function resolve(dir) {
  return path.join(__dirname, dir);
}
const env = process.env;

let baseUrl = "./";
let PAC_FILE = "http://txp-01.tencent.com/proxy.pac";
try {
  PAC_FILE = (process.platform === "win32"
    ? child_process
        .execSync(
          'REG QUERY HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet" "Settings /v AutoConfigURL'
        )
        .toString()
        .split("\r\n")
    : child_process
        .execSync("system_profiler SPNetworkDataType")
        .toString()
        .split("\n")
  )
    .filter((row) => /(Auto Configure URL|AutoConfigURL)/.test(row))[0]
    .match(/^.+(http.+)$/)[1];
} catch (e) {
  console.error(e);
  console.error("获取用户PAC文件失败");
}
console.log(`using pac file ${PAC_FILE}`);

module.exports = {
  baseUrl: baseUrl, // 根据你的实际情况更改这里
  lintOnSave: true,
  devServer: {
    publicPath: baseUrl, // 和 baseUrl 保持一致
  },
  productionSourceMap: false,
  chainWebpack: (config) => {
    if (env.VUE_BUILD_ENV === "analyz") {
      config
        .plugin("webpack-bundle-analyzer")
        .use(BundleAnalyzerPlugin)
        .init((Plugin) => new Plugin());
    }

    //忽略的打包文件
    config.externals({
      vue: "Vue",
      "vue-router": "VueRouter",
      vuex: "Vuex",
      axios: "axios",
      "element-ui": "ELEMENT",
    });

    // set svg-sprite-loader
    config.module
      .rule("svg")
      .exclude.add(resolve("src/icons"))
      .end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();

    const entry = config.entry("app");
    entry.add("babel-polyfill").end();
    entry.add("classlist-polyfill").end();

    if (env.NODE_ENV === "development") {
      entry.add("@/mock").end();
    }
  },
  transpileDependencies: ["avue-plugin-transfer", "avue-plugin-ueditor"],
  //配置转发代理
  devServer: {
    open: true,
    host: "dev.vastscene.icebartech.com",
    https: true,
    port: 8080,
    proxy: {
      "/api": {
        // agent: new PacProxyAgent(PAC_FILE),
        target: "https://vastscene.icebartech.com/",
        changeOrigin: true,
        secure: false,
      },
    },
  },
};
